#include <stdio.h>
#include <easyx.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>

typedef struct {
	int x, y;
	int vx, vy;
	int r;
	int barLeft, barTop, barRight, barBotton;
}GameData;

void reset(GameData* gdata)
{
	gdata->x = rand() % 401 - 200;
	gdata->y = rand() % 301 - 150;
	gdata->vx = 5;
	gdata->vy = 5;
	if (rand() % 2 == 0)
	{
		gdata->vx = -gdata->vx;
	}
	if (rand() % 2 == 0)
	{
		gdata->vy = -gdata->vy;
	}
	gdata->r = 50;
	gdata->barLeft = -150;
	gdata->barTop = -280;
	gdata->barRight = 150;
	gdata->barBotton = -300;
}


int main()
{
	    initgraph(800, 600);
		setorigin(400, 300);
		setaspectratio(1, -1);
		setbkcolor(HSVtoRGB(216, 0.26f, 0.95f));
		cleardevice();
		setfillcolor(WHITE);
	
		srand((unsigned int)time(NULL));
	
		GameData gdata;
		reset(&gdata);
	
		int vw = 20;
		while (1)
		{
			cleardevice();
			solidcircle(gdata.x, gdata.y, gdata.r);
			solidrectangle(gdata.barLeft, gdata.barTop, gdata.barRight, gdata.barBotton);
			Sleep(40);
			if (gdata.y >= 300 - gdata.r)
			{
				gdata.vy = -gdata.vy;
			}
			else if (gdata.x >= 400 - gdata.r || gdata.x <= -400 + gdata.r)
			{
				gdata.vx = -gdata.vx;
			}
			if (gdata.x >= gdata.barLeft && gdata.x <= gdata.barRight && gdata.y <= gdata.barTop + gdata.r)
			{
				gdata.vy = -gdata.vy;
				if (gdata.vx <= 50 && gdata.vy <= 50)
				{
					gdata.vx += 3;
					gdata.vy += 3;
					if (gdata.barRight - gdata.barLeft >= 50)
					{
						gdata.barLeft++;
						gdata.barRight--;
						vw += 2;
					}
				}
			}
			gdata.x += gdata.vx;
			gdata.y += gdata.vy;
			if (_kbhit())
			{
				char c = _getch();
				if (c == 'a')
				{
					if (gdata.barLeft > -400)
					{
						gdata.barLeft -= vw;
						gdata.barRight -= vw;
					}
				}
				else if (c == 'd')
				{
					if (gdata.barRight < 400)
					{
						gdata.barLeft += vw;
						gdata.barRight += vw;
					}
				}
			}
			if (gdata.y <= -300)
			{
				reset(&gdata);
				vw = 20;
			}
		}
		closegraph();
		return 0;
}